package com.mdxq.mall.product.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.mdxq.mall.product.entity.Brand;
import com.mdxq.mall.product.mapper.BrandMapper;
import com.mdxq.mall.product.service.BrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * 品牌表 服务层实现。
 *
 * @author zdy
 * @since 2025-01-12
 */
@Service
@Slf4j
//放在service之前，提供当前类下统一的缓存设置,包括缓存管理器
@CacheConfig(cacheNames = "brandCache",cacheManager = "cacheManager")
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand>  implements BrandService{
    @Override
    //将当前方法返回的对象（结果）存到缓存中
    @Cacheable(key = "#id")//先看缓存，没有就数据库
//    @CachePut(value = "brandCache",key = "#id")和上面任选其一  //无论缓存有没有都执行这个方法，再进行缓存
    public Brand getById(Serializable id) {
//        log.info("从mysql中查到了这条记录");
        return super.getById(id);
    }

    @Override
    @CacheEvict(key = "#entity.id")
    //brandCache该缓存都清理掉
//    @CacheEvict(value = "brandCache",allEntries = true)//执行该方法时将缓存中对应的键清理掉
    public boolean updateById(Brand entity) {
        return super.updateById(entity);
    }

    @Override
    @CacheEvict(allEntries = true)
//    @CacheEvict(value = "brandCache",key = "#entity.id")
    public boolean removeById(Brand entity) {
        return super.removeById(entity);
    }

    @Override
//    @Cacheable(value = "brandCache",key = "'list'") //list不加单引号就会当成属性
    public List<Brand> list() {
        return super.list();
    }
}
